package web.commons;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow;

//      <div style="display: none;" wicket:id="modal"></div>
public abstract class ConfirmModalWindow extends ModalWindow {
    protected ConfirmationAnswer answer;

    protected abstract void onConfirm(AjaxRequestTarget target);

    protected abstract void onCancel(AjaxRequestTarget target);

    private Object[] userObjects;

    public ConfirmModalWindow(String id, String modalMessageText) {
        super(id);
        answer = new ConfirmationAnswer(false);

        setCookieName(id);
        setContent(new YesNoPanel(getContentId(), modalMessageText, this, answer));
        setWindowClosedCallback(new ModalWindow.WindowClosedCallback() {

            public void onClose(AjaxRequestTarget target) {
                if (answer.isAnswer()) {
                    onConfirm(target);
                } else {
                    onCancel(target);
                }
            }
        });
    }

    public void show(final AjaxRequestTarget target, Object... object) {
        setUserObjects(object);
        show(target);
    }

    public Object[] getUserObjects() {
        return userObjects;
    }

    public void setUserObjects(Object[] userObjects) {
        this.userObjects = userObjects;
    }
}
